struct Solution {}

fn main() {}

impl Solution {
    pub fn reverse_str(s: String, k: i32) -> String {
        let mut start = 0;
        let mut end = 2 * k;
        let mut ret = String::new();

        // 处理循环部分
        while end < s.len() as i32 {
            let x: &str = &s[start as usize..(start + k) as usize];
            ret += &Self::reverse(&x);
            let y: &str = &s[(start + k) as usize..(start + 2 * k) as usize];
            ret += y;
            start += 2 * k;
            end += 2 * k;
        }

        // 剩余的分一段处理
        if start + k >= s.len() as i32 {
            let z = &s[start as usize..];
            ret += &Self::reverse(&z);
            return ret;
        }

        // 剩余的分两部分处理
        if start + k <= s.len() as i32 {
            let z = &s[start as usize..(start + k) as usize];
            ret += &Self::reverse(&z);
            start += k;
        }
        ret += &s[(start) as usize..s.len()];
        ret
    }

    fn reverse(s: &&str) -> String {
        let chars: Vec<char> = (*s.chars().rev().collect::<Vec<_>>()).to_vec();
        chars.into_iter().collect()
    }
 }